package com.shenbianyun.baiwei;


import com.alibaba.fastjson.JSONObject;
import com.shenbianyun.bosskgpay.entity.RequestMessage;
import com.shenbianyun.bosskgpay.entity.pay.PaymentReqModel;
import com.shenbianyun.bosskgpay.utils.*;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：张一凡
 * @date ：2024年02月26日 11:30:34
 * @description： 百威付款
 */
public class BWPaymentServiceJ extends AbstractJavaSamplerClient {


    //预发
    //public final String OMS_TEST_URL = "http://uatapi.merchant.serviceshare.com/clientapi/clientBusiness/common";
    //public final String INTER_KEY = "FACAE068BFC2B748C990755772BBADDB";
    //public final String MER_PRIVATE_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMhyR4I/yTdDrrI6C7HhHEH20HtbkF+wrFWoG7Rk18TatN7L6rLcW3mV3fPZUM07W7pKEGVz01grrl/OoKHbKWAEm7n+WdRXsBJ+VinVSdI0IynquDjh6qTUM8gBrPaU3KpwSdVqdY51foQw4mr7h93Bq8W6GOkgBiosQYd2yYF5AgMBAAECgYEAk6ZyfEQmIPpPLePKB45tdT2slqF8+K+4UiRRvBDoS45a2x7UVEoza510cBItDfBighfaehDWpkUO3m3jLza0A0PHmbryNsyrw0HSLiPJVaf1/wP2jvI94veXIlL1ttyKsq1kSVhgUuRS2anq/IXdaF2G1NE/rmeGatCPg0kcp7ECQQDinjgD1z8D0y+7duL8HD3+sCL94VXlGrj2DrzI75zXyuf4CBqSSRSX1lfy3HHjw5alVnmUqVQPyIK/P50JWJxlAkEA4m9iy1czgowK7DTUWOGXMVobzZdjvS168FllIJxrj3dhAvoT6MduotUoZ3e+2e9F3AoGiLGh4+NM7lmciNCthQJAb33uCx2z9/rHyhA0nlp6uboIjSV2r+/UveIESUtxu80tER9Ti1L/GbCvtmF0et04WYHQ8S3ZmKZKhTMT3IxG7QJAMoRBGTZaIozVvv7GRRIHJhaBT2uv77ueytJW0mCzSm2KshxSpsMfdqpxeivn4z0Ta9S8wnBGus2yExbg6lEL9QJBAJG8lqFuPfpzHtoCd7Xbv0GWJ+K/modlWPWbxANJQw9OL2sqQ2Was/Ipflyggu+gDDkkhePe77YkSdlAJxVNbzY=";
    //public final String MER_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIckeCP8k3Q66yOgux4RxB9tB7W5BfsKxVqBu0ZNfE2rTey+qy3Ft5ld3z2VDNO1u6ShBlc9NYK65fzqCh2ylgBJu5/lnUV7ASflYp1UnSNCMp6rg44eqk1DPIAaz2lNyqcEnVanWOdX6EMOJq+4fdwavFuhjpIAYqLEGHdsmBeQIDAQAB";
    //public final long MER_ID = 1645114192235329L;
    //public final long providerId = 34156;
    //public final long TASK_ID = 1610278112283287739L;


    //测试
    public final String OMS_TEST_URL = "http://testgateway.serviceshare.com/testapi/clientapi/clientBusiness/common";

    public final String INTER_KEY = "930A7CE165CB2D27F62A34862A397618";
    public final String MER_PRIVATE_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMhyR4I/yTdDrrI6C7HhHEH20HtbkF+wrFWoG7Rk18TatN7L6rLcW3mV3fPZUM07W7pKEGVz01grrl/OoKHbKWAEm7n+WdRXsBJ+VinVSdI0IynquDjh6qTUM8gBrPaU3KpwSdVqdY51foQw4mr7h93Bq8W6GOkgBiosQYd2yYF5AgMBAAECgYEAk6ZyfEQmIPpPLePKB45tdT2slqF8+K+4UiRRvBDoS45a2x7UVEoza510cBItDfBighfaehDWpkUO3m3jLza0A0PHmbryNsyrw0HSLiPJVaf1/wP2jvI94veXIlL1ttyKsq1kSVhgUuRS2anq/IXdaF2G1NE/rmeGatCPg0kcp7ECQQDinjgD1z8D0y+7duL8HD3+sCL94VXlGrj2DrzI75zXyuf4CBqSSRSX1lfy3HHjw5alVnmUqVQPyIK/P50JWJxlAkEA4m9iy1czgowK7DTUWOGXMVobzZdjvS168FllIJxrj3dhAvoT6MduotUoZ3e+2e9F3AoGiLGh4+NM7lmciNCthQJAb33uCx2z9/rHyhA0nlp6uboIjSV2r+/UveIESUtxu80tER9Ti1L/GbCvtmF0et04WYHQ8S3ZmKZKhTMT3IxG7QJAMoRBGTZaIozVvv7GRRIHJhaBT2uv77ueytJW0mCzSm2KshxSpsMfdqpxeivn4z0Ta9S8wnBGus2yExbg6lEL9QJBAJG8lqFuPfpzHtoCd7Xbv0GWJ+K/modlWPWbxANJQw9OL2sqQ2Was/Ipflyggu+gDDkkhePe77YkSdlAJxVNbzY=";
    public final String MER_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIckeCP8k3Q66yOgux4RxB9tB7W5BfsKxVqBu0ZNfE2rTey+qy3Ft5ld3z2VDNO1u6ShBlc9NYK65fzqCh2ylgBJu5/lnUV7ASflYp1UnSNCMp6rg44eqk1DPIAaz2lNyqcEnVanWOdX6EMOJq+4fdwavFuhjpIAYqLEGHdsmBeQIDAQAB";
    public final long MER_ID = 1676619392976687L;
    public final long providerId = 30481;
    public final long TASK_ID = 1607707542044405927L;



    public final String API_VERSION = "V1.0";
    public final String CHARSET_UTF_8 = "UTF-8";
    public final String funCode = "6032";
    public RequestMessage rm = new RequestMessage();//公共参数
    public PaymentReqModel paymentReqModel = new PaymentReqModel();//批量付款参数
    public List payItemsList = new ArrayList();//批量付款明细
    public PaymentReqModel.PayItems payItems = new PaymentReqModel.PayItems(); //1用户

    public static void main(String[] args) {

        try {
            System.out.println(new BWPaymentServiceJ().runBWPaymentServiceJ());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public String runBWPaymentServiceJ() throws Exception {

        Long id = SnowflakeIdGeneratorFactory.getInstance().nextId();
        rm.setReqId("test" + id);
        rm.setFunCode(funCode);
        rm.setMerId(MER_ID);
        rm.setVersion(API_VERSION);


        /**--------————————————业务参数开始——————————————————**/
        //商户批次号
        paymentReqModel.setMerBatchId("batc" + id);
        paymentReqModel.setTaskId(TASK_ID);
        paymentReqModel.setProviderId(providerId);


        //金额（分）
        payItems.setAmt(1L);
        //备注
        payItems.setMemo("api压测");
        //商户订单号，同批次内确保唯一不可重复。
        payItems.setMerOrderId("orde" + id);
        //付款方式 百威固定支付宝。1：支付宝
        payItems.setPaymentType(1);
        //百威用户ID
        payItems.setMerUserId("123#=vip-1");
        /**异步通知地址**/
        //payItems.setNotifyUrl("http://192.168.48.98:8081/notify/doBusiness");
        //保存批量付款明细
        payItemsList.add(payItems);
        paymentReqModel.setPayItems(payItemsList);
        /**--------———————————新业务结束——————————————————**/


        String json = JsonUtils.toJson(paymentReqModel);
        //System.out.println("请求未加密数据:" + json);

        //加密处理
        byte[] bs = DESUtils.encrypt(json.getBytes(CHARSET_UTF_8), INTER_KEY);
        String reqDataEncrypt = Base64Utils.encodeToString(bs);
        rm.setReqData(reqDataEncrypt);
        rm.setSign(RSAUtils.sign(reqDataEncrypt, MER_PRIVATE_KEY));
        String reqStr = JsonUtils.toJson(rm);
        //System.out.println("请求数据：" + reqStr);

        //发送请求
        String resData = HttpClientHelper.sendPost(OMS_TEST_URL, "application/json", reqStr, "POST", CHARSET_UTF_8);
        //System.out.println("响应数据：" + resData + rm.getReqId());


        ////返回信息：DES 解密
        //ResponseMessage responseMessage = JSON.parseObject(resData, ResponseMessage.class);
        ///** 2、Base64解编码 resDATA对象解密 **/
        //String respData = responseMessage.getResData();
        //byte[] base64bs = Base64Utils.decode(respData.getBytes());
        ///** 3、DES 解密 **/
        //String resultData = DESUtils.decryptToString(base64bs, MerInfoConstant.INTER_KEY);
        //responseMessage.setResData(resultData);
        //System.out.println("解密结果："+responseMessage);

        return resData;
    }


    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        SampleResult sampleResult = new SampleResult();
        String resData = "";
        try {
            sampleResult.sampleStart();
            resData = runBWPaymentServiceJ();
            //断言
            JSONObject jsonObject1 = JSONObject.parseObject(resData);
            if (jsonObject1.get("resCode").equals("0000") || jsonObject1.get("resMsg").toString().contains("频繁")) {
                sampleResult.setResponseOK();
                sampleResult.setResponseData(true  + " " + jsonObject1.get("resMsg"), SampleResult.TEXT);
                System.out.println("批量付款结果:" + true + " " + jsonObject1.get("resMsg"));
            } else {
                sampleResult.setSuccessful(false);
                sampleResult.setResponseData(false + " " + jsonObject1.get("resMsg"), SampleResult.TEXT);
                System.out.println("批量付款结果:" + false + " " + jsonObject1.get("resMsg")+ " reqId:" + rm.getReqId());
            }
        } catch (Exception e) {
            sampleResult.setSuccessful(false);
            sampleResult.setResponseData(resData, SampleResult.TEXT);
            sampleResult.setResponseMessage(e.toString());
        } finally {
            sampleResult.sampleEnd();
        }

        return sampleResult;
    }



}
